package com.crm.system.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.redis.service.cluePoolManager.CluePoolStatisticsRedisService;
import com.crm.service.cluePoolManager.ClueDbService;
import com.crm.system.service.cluePoolManager.ClueFollowService;
import com.crm.system.service.cluePoolManager.CluePoolStatisticsService;
import com.crm.system.service.cluePoolManager.ClueReceiveService;
import com.crm.system.service.cluePoolManager.ClueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/2/2 10:57
 */
@Service
public class CluePoolStatisticsServiceImpl implements CluePoolStatisticsService {

    @Autowired
    private CluePoolStatisticsRedisService poolStatisticsRedisService;
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueFollowService clueFollowService;
    @Autowired
    private ClueReceiveService clueReceiveService;

    @Override
    public JSONObject cluePoolLeftStatistics(String cluePoolId) {
        JSONObject result = new JSONObject();
        //线索总数量
        long clueTotalCount = clueService.selectClueCountByPublicPoolId(cluePoolId);
        //线索总领取数
        long clueReceiveCount = clueReceiveService.selectClueReceiveByPoolId(cluePoolId);
        //线索0次领取数量
        long clueZeroTimeReceiveCount = clueService.clueZeroTimeReceiveCount(cluePoolId);
        //线索池累计使用人数
        Integer poolUsageCount = poolStatisticsRedisService.getUsageCountById(cluePoolId);
        //线索领取数
        long receiveCount = clueService.selectDistributedOrReceiveCount(cluePoolId);
        //线索闲置数
        long idleCount = clueTotalCount-receiveCount;

        //baijian  线索总转化数
        long conversionsCount = clueService.selectClueConverCountByPublicPoolId(cluePoolId);

        //线索池总跟进数
        long followCount = clueFollowService.selectClueFollowCountByPoolId(cluePoolId);
        result.put("clueTotalCount",clueTotalCount);
        result.put("clueReceiveCount",clueReceiveCount);
        result.put("clueZeroTimeReceiveCount",clueZeroTimeReceiveCount);
        result.put("poolUsageCount",poolUsageCount);
        result.put("idleCount",idleCount);
        result.put("conversionsCount",conversionsCount);
        result.put("followCount",followCount);
        return result;
    }

    @Override
    public Boolean addCluePoolUsageCount(String cluePoolId) {
        return poolStatisticsRedisService.setUsageCountById(cluePoolId);
    }

    @Override
    public List<JSONObject> cluePoolReceiveConversionStatistics(String cluePoolId, Integer monthCount) throws ParseException {
        List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM");
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(new Date());
        nowCal.add(Calendar.MONTH, -1 * monthCount);
        for (int i = 0; i < monthCount; i++) {
            nowCal.add(Calendar.MONTH, 1);
            String yearMonth = timeSdf.format(nowCal.getTime());
           // int currentYearMonth = Integer.valueOf(timeSdf.format(new Date()));
            JSONObject mouthResult = new JSONObject();
            mouthResult.put("yearMonth",yearMonth);
            //如果是之前的月份,可以先从缓存获取信息（以后放开，先注释掉。以后加redis读取）
/*            if (currentYearMonth > yearMonth) {
                result = getCluePoolReceiveStatisticsForOneMonthFromRedis(cluePoolId,yearMonth);
            } else {
                result = getCluePoolReceiveStatisticsForOneMonthFromDb(cluePoolId, yearMonth);
            }*/
            long receiveCountResult = getCluePoolReceiveStatisticsForOneMonthFromDb(cluePoolId, yearMonth);
            mouthResult.put("receiveCount",receiveCountResult);

            long conversionCountResult = clueService.getCluePoolConversionStatisticsForOneMonth(cluePoolId, yearMonth);
            mouthResult.put("conversionCount",conversionCountResult);
            jsonObjects.add(mouthResult);

        }
        return jsonObjects;
    }

    public long getCluePoolReceiveStatisticsForOneMonthFromDb (String cluePoolId,String yearMonth){
        long result = clueReceiveService.selectReceiveCountForCurrentMonthByPoolId(cluePoolId,yearMonth);
        return result;
    }
}
